<?php

namespace app\divination\controller;

use \app\auth\controller\Base;
use \app\common\model\DivinationNote;
use think\Cache;
use \think\Db;
use app\check\controller\Check;

/**
 * Class Index
 * @title 提问注释接口
 * @url  /note/divination
 * @desc  提问注释相关接口<br/>当前模块所有请求地址一样，都是http://localapi.goddessxzns.com/note/divination
 * @version 1.0.0
 * @readme /doc/md/divination/note/
 */
class Note extends Check
{
    protected $restActionList = ['index', 'save', 'update', 'delete'];

    /**
     * @title 注释列表
     * @url /note/divination
     * @desc 若无注释，则返回的json中data不存在<br/>http://localapi.goddessxzns.com/note/divination
     * @readme /doc/md/divination/note/index.md
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $uid = $this->user['id'];

        $did = input('did/d', 0);

        if (!$uid)
        {
            return show(config('code.error'), '用户不存在');
        }

        if (!$did)
        {
            return show(config('code.error'), '提问不存在');
        }

        $is_use_cache = 1;
        if ($is_use_cache)
        {
            $data = (new DivinationNote())->getListAll(['did' => $did, 'uid' => $uid]);
        } else
        {
            $data = DivinationNote::field('id,did,content,create_time')->where(['did' => $did, 'uid' => $uid])->order('create_time DESC')->select();
        }


        return show(config('code.success'), '成功', $data);
    }

    /**
     * @title 添加注释
     * @desc http://localapi.goddessxzns.com/note/divination
     * @readme /doc/md/divination/note/save.md
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function save()
    {
        $data['access_token'] = input('access_token/s');

        $data['content'] = htmlspecialchars(trim(input('content')));

        $data['did'] = input('did/d', 0);

        $data['uid'] = $this->user['id'];

        $validate = validate('DivinationNote');

        if ($validate->check($data) !== true)
        {
            return show(config('code.error'), $validate->getError());
        }
        unset($data['access_token']);
        $time = time();
        $data['create_time'] = $time;
        $data['last_time'] = $time;

        if ($data['id'] = (int)(Db::table('wechat_backup_07_18_14_43_00.app_divination_note')->insertGetId($data)))
        {
            unset($data['uid']);
            unset($data['last_time']);

            Cache::rm(DivinationNote::REDIS_KEY_IDS . $data['did']);

            return show(config('code.success'), '成功', $data);
        }
        return show(config('code.error'), '添加失败');
    }

    /**
     * @title 更新笔记注释
     * @desc http://localapi.goddessxzns.com/note/divination <br/>参数传递使用form-data无效，请使用其他方式 如x-www-form-urlencoded
     * @readme /doc/md/divination/note/update.md
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     */
    public function update()
    {
        $data['content'] = htmlspecialchars(trim(input('content')));

        $data['did'] = input('did/d', 0);
        $data['id'] = input('id/d', 0);

        $data['uid'] = $this->user['id'];

        $validate = validate('DivinationNote');

        if ($validate->check($data) !== true)
        {
            return show(config('code.error'), $validate->getError());
        }
        unset($data['access_token']);

        $divination = new DivinationNote();

        if ($divination->allowField(['content'])->isUpdate(true)->save($data, ['uid' => $data['uid'], 'did' => $data['did'], 'id' => $data['id']]))
        {
            Cache::rm(DivinationNote::REDIS_KEY_DETAIL . $data['id']);
            return show(config('code.success'), '成功', $data);
        }
        return show(config('code.error'), '更新失败！');
    }

    /**
     * @title 删除笔记注释
     * @readme /doc/md/divination/note/delete.md
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     */
    public function delete()
    {
        $did = input('did/d', 0);
        $id = input('id/d', 0);

        $uid = $this->user['id'];

        DivinationNote::destroy(['uid' => $uid, 'did' => $did, 'id' => $id]);
        Cache::rm(DivinationNote::REDIS_KEY_IDS . $did);
        Cache::rm(DivinationNote::REDIS_KEY_DETAIL . $id);

        return show(config('code.success'), '成功');
    }

    /**
     * 参数规则
     * @name 字段名称
     * @type 类型
     * @require 是否必须
     * @default 默认值
     * @desc 说明
     * @range 范围
     * @return array
     */
    public static function getRules()
    {
        $rules = [
            'index' => [
                'access_token' => ['name' => 'access_token', 'type' => 'string', 'require' => 'true', 'default' => '无', 'desc' => '接口访问凭证', 'range' => '32位',],
                'ucid' => ['name' => 'ucid', 'type' => 'string', 'require' => 'true', 'default' => '无', 'desc' => '用户ucid', 'range' => '32位',],
                'did' => ['name' => 'did', 'type' => 'int', 'require' => 'true', 'default' => '无', 'desc' => '提问记录id', 'range' => '',],
            ],
            'save' => [
                'access_token' => ['name' => 'access_token', 'type' => 'string', 'require' => 'true', 'default' => '无', 'desc' => '接口访问凭证', 'range' => '32位',],
                'ucid' => ['name' => 'ucid', 'type' => 'string', 'require' => 'true', 'default' => '无', 'desc' => '用户ucid', 'range' => '32位',],
                'did' => ['name' => 'did', 'type' => 'int', 'require' => 'true', 'default' => '无', 'desc' => '提问记录id', 'range' => '',],
                'content' => ['name' => 'content', 'type' => 'string', 'require' => 'true', 'default' => '无', 'desc' => '当前提问笔记内容', 'range' => '300字以内！',],
            ],
            'update' => [
                'access_token' => ['name' => 'access_token', 'type' => 'string', 'require' => 'true', 'default' => '无', 'desc' => '接口访问凭证', 'range' => '32位',],
                'ucid' => ['name' => 'ucid', 'type' => 'string', 'require' => 'true', 'default' => '无', 'desc' => '用户ucid', 'range' => '32位',],
                'did' => ['name' => 'did', 'type' => 'int', 'require' => 'true', 'default' => '无', 'desc' => '提问记录id', 'range' => '不限',],
                'id' => ['name' => 'id', 'type' => 'int', 'require' => 'true', 'default' => '无', 'desc' => '注释id', 'range' => '不限',],
                'content' => ['name' => 'content', 'type' => 'string', 'require' => 'true', 'default' => '无', 'desc' => '当前提问笔记内容', 'range' => '300字以内',],
            ],
            'delete' => [
                'access_token' => ['name' => 'access_token', 'type' => 'string', 'require' => 'true', 'default' => '无', 'desc' => '接口访问凭证', 'range' => '32位',],
                'ucid' => ['name' => 'ucid', 'type' => 'string', 'require' => 'true', 'default' => '无', 'desc' => '用户ucid', 'range' => '32位',],
                'did' => ['name' => 'did', 'type' => 'int', 'require' => 'true', 'default' => '无', 'desc' => '提问记录id', 'range' => '不限',],
                'id' => ['name' => 'id', 'type' => 'int', 'require' => 'true', 'default' => '无', 'desc' => '注释id', 'range' => '不限',],
            ],
        ];
        return $rules;
    }
}
